package com.hk.config;

import com.hk.core.json.JackJsonHttpMessageConverterEx;
import com.hk.core.json.JsonFilterHandler;
import com.hk.interceptor.SessionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @Author xuebinlu
 * @Date 2018/5/9 15:39
 * @Version 1.0
 * @Description springmvc 该配置类
 **/
@Configuration
@Slf4j
public class NewWebMvcConfigurationSupport extends WebMvcConfigurationSupport {

    @Autowired
    private SessionInterceptor sessionInterceptor;

    @Autowired
    private JsonFilterHandler jsonFilterHandler;

    @Autowired
    JackJsonHttpMessageConverterEx jackJsonHttpMessageConverterEx;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

//        设置好读取静态文件的位置：

//        spring.resources.static-locations=classpath:/META-INF/resources/,
// classpath:/resources/,classpath:/static/,classpath:/public/
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");


       super.addResourceHandlers(registry);
    }

    /**
     * 扩展添加自定义的信息转换类，该方法不会覆盖默认信息转换类
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.extendMessageConverters(converters);
        converters.add(0,jackJsonHttpMessageConverterEx);
    }

    @Override
    protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(jsonFilterHandler);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(sessionInterceptor).addPathPatterns("/**");
//        super.addInterceptors(registry);
//    }

}
